<?php
/**
 * 登录相关
 * @author: yanxs <541139655@qq.com>
 * @version: 1.0
 * @date: 2021/4/1
 */

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Backend\LoginRequest;
use App\Exceptions\CommonException;
use App\Repositories\PrtAdminRepository;
use App\Repositories\YxsAdminRepository;
use App\Services\Auth\AuthInterface;
use Swoole\Coroutine\Client;
use  Swoole\Coroutine;

class LoginController extends BaseController
{
    protected $viewPath = 'common.login';  //当前控制器所的view所在的目录
    protected $modules = 'auth';        //当前控制器所属模块
    protected $sysId = 'backend';        //当前控制器所属模块
    protected $noNeedRight = ['*'];     //不需要做权限验证

    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 页面视图
     * @return mixed
     */
    public function index()
    {
       // phpinfo();exit;
        //测试swoole客户端
//        $client = new \Swoole\Client(SWOOLE_SOCK_TCP);
//        if (!$client->connect('127.0.0.1', 9501, -1)) {
//            exit("connect failed. Error: {$client->errCode}\n");
//        }
//        $client->send("hello world\n");
//        //echo $client->recv();
//        $client->close();

        return view($this->viewPath);
    }

    /**
     *  登录逻辑
     * @param LoginRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function dologin(LoginRequest $request)
    {
        try {
            $loginType = $request->input('login_type') ?? LOGIN_TYPE_PASSWORD;
            app()->instance('login_type',$loginType); //以参数来指定登录方式
            //组状登录数据
            $data = [
                'username' =>  $request->input('username'),
                'password' => $request->input('password'),
                'captcha'  => $request->input('captcha')
            ];
            //登录验证
            $objAuth = app(AuthInterface::class);
            $objAuth->setUserRepository(PrtAdminRepository::class); //将依赖的账号仓库通过方法注入
            $objAuth->verify($data);
            return $this->jsonSuccess([]);

        } catch (CommonException $e) {
            return $this->jsonFailed($e->getMessage());
        }
    }

}